<template>
  <div class="waste-ladel-detail" v-loading="load">
    <template v-if="form.typeL.length > 0 && ks_lis.length > 0">
      <el-form ref="form" :model="form" label-width="110px">

        <el-form-item label="科室:" class="">
          <div class="ks">
            <div class="ks-con">
              {{form.ks}}
            </div>
            <el-button type="success" plain @click="handleClickShowKs">更换</el-button>
          </div>
        </el-form-item>

        <!-- <div class="waste-type"> -->
          <el-form-item v-for="(item, index) in form.typeL" :key="index" :label="item.name">
            <div class="waste-type-num">
              <el-input-number size="small" v-model="item.num" @change="handleChange" :min="0"  label="申领数量"></el-input-number> 
              <!-- <span class="red">卷</span>  -->
            </div>
          </el-form-item>
        <!-- </div> -->
      
      <el-form-item label="备注" class="des">
          <div>
            <el-input
              type="textarea"
              placeholder="请输入备注"
              v-model="form.remark">
            </el-input>
          </div>
        </el-form-item>

      </el-form>
      
    
      <!-- <div class="tips" style="text-align: center;">说明：每卷标签800张</div> -->

      <div class="handle-box">
        
        <template v-if="is_crated">
          <el-button type="primary" @click="handleSureconfirm">确认上传</el-button>
        </template>

        <template v-else>
          <el-button type="primary" @click="handleSureconfirm">确认修改</el-button>
          <el-button type="danger" @click="visible_cancel = true">删除申请</el-button>
        </template>
      </div>
    </template>
    <div v-else class="empty">暂无数据</div>

    <el-dialog custom-class="select-ks-dialog"  append-to-body title="选择申领科室" :visible.sync="visible_ks" :show-close="false" center >
      <SelectKs 
        :data="ks_lis"
        :curr="form.ks"
        v-if="visible_ks"
        @onselect="handleChangeKs"
      />
    </el-dialog>

    

    <el-dialog title="删除申请确认" append-to-body custom-class="cancel-dialog"  width="500px" top="30vh" :visible.sync="visible_cancel"  :before-close="handleCancelDel">
      <el-input
        type="textarea"
        placeholder="请输入删除申请的原因"
        v-model="form.cancel_des">
      </el-input>
      <div class="handle-box">
        <el-button type="danger" @click="handleDel">确认</el-button>
        <el-button type="info" plain @click="handleCancelDel">取消</el-button>
      </div>
      
    </el-dialog>
  </div>
</template>

<script>
import SelectKs from "../components/selectKs.vue"
import { wastecategoryUrl, getWasteKsUrl, postConfirmCreateUrl, postWasteLabelModUrl } from "../../../common/ajax"
import { mapActions } from "vuex"
import store from '../../../store'
export default {
  components:{
    SelectKs
  },
  props:{
    data: Object
  },
  data(){
    return {
      form:{
        ks:"",
        typeL:[],
        remark:"",
        cancel_des:""
      },
      ks_lis:[],
      is_crated: false,
      load: false,
      visible_ks: false,
      is_handle: false,
      visible_cancel: false,
    }
  },
  created(){
    this.getWasteUserKs()
    if(this.data && this.data.id){
      //编辑
      this.is_crated = false
      this.form = JSON.parse(JSON.stringify(this.data))
      this.form["typeL"] = this.data.kind
    }else {
      //创建
      this.is_crated = true
      this.load = true
      this.handleGetType()
    }
    
  },
  mounted(){
    this.handleGetKs()
  },
  methods:{
    handleGetType(){
      this.form.typeL = []
      jQuery.ajax({
        url: wastecategoryUrl,
        type: 'GET',
        success: res => {
          if(res.success){
            let typel = []
            res.data.forEach(v => {
              if(v.hang != 1){
                typel.push({name:v.name , num: 0 })
              }
            });
            typel.push({name: "空白",  num: 0})
            this.form.typeL = typel
          }else{
            throwErrorTips(res.msg)
          }
          this.load = false
        },
        error: ()=>{
          this.load = false
          throwErrorTips("网络超时")
        }
      })
    },
    handleGetKs(s){
      this.ks_lis = []
      jQuery.ajax({
        url: getWasteKsUrl,
        type: 'GET',
        success: res => {
          if(res.success){
            let ksl = []
            res.data.forEach(v => {
              if(v.hang != 1){
                ksl.push(v)
              }
            });
            this.ks_lis = ksl
            if(s){
              this.visible_ks = true
            }else {
              if(this.is_crated){
                if(store.state.hwUserKs && store.state.hwUserKs[0]){
                  this.form.ks = store.state.hwUserKs[0].department
                }else if(this.ks_lis.length> 0){
                  this.form.ks = this.ks_lis[0].address
                }
              }
              
            }
            
          }else{
            throwErrorTips(res.msg)
          }
          this.is_handle = false
        },
        error: ()=>{
          this.is_handle = false
          throwErrorTips("网络超时")
        }
      })
    },
    handleClickShowKs(){
      if(this.is_handle){ return}
      this.is_handle = true
      this.handleGetKs(1)
    },
    handleChangeKs(val){
      this.form.ks = val
      this.visible_ks = false
    },

    handleChange(){
      
    },

    handleSureconfirm(){
      if(!this.form.ks){
        throwErrorTips("申领科室不能为空")
        return
      }
      let noNum = 0,
        kind = []
      this.form.typeL.forEach((v)=>{
        kind.push({name:v.name,num:v.num.toString()})
        if(v.num != 0){
          noNum++
        }
      })
      if(noNum == 0){
        throwErrorTips("申领数不能都为0")
        return
      }
      if(this.is_handle){return}
      this.is_handle = true
      if(this.is_crated){
        this.handleConfirmAjax(kind,"1","创建标签申领")
      }else {
        let logs = ""
        if(this.form.ks != this.data.ks){
          logs += "将科室 "+this.data.ks+" 修改为 " + this.form.ks+'；'
        }
        kind.forEach((v)=>{
          this.form.kind.forEach(ele=>{
            if(v.name == ele.name && v.num.toString() != ele.num.toString()){
              logs += "将 "+v.name+" 申领量 "+ele.num+" 修改为 " + v.num+'；'
            }
          })
        })
        if(this.form.remark != this.data.remark){
          logs += "将备注 "+this.data.remark+" 修改为 " + this.form.remark+'；'
        }
        if(!logs){
          throwErrorTips("没有修改")
          this.is_handle = false
          return 
        }
        this.form.kind = kind 
        this.modAjax(this.form,"2",logs)
      }
    },
    handleConfirmAjax(kind,skind,log_des){
      jQuery.ajax({
        url: postConfirmCreateUrl,
        type: 'post',
        data: JSON.stringify({
          ks:this.form.ks,
          remark: this.form.remark,
          kind: kind,
          skind:skind,
          log_des:log_des
        }),
        contentType: "application/json",
        success: res => {
          if(res.success){
            this.$Message({
              message: "成功",
              type: 'success',
            })
            this.$emit("onclose")
          }else {
            throwErrorTips(res.msg)
          }
          this.is_handle = false
        },
        error:()=>{
          this.is_handle = false
          throwErrorTips("网络超时")
        }
      })
    },
    handleCancelDel(){
      this.visible_cancel = false
      this.form.cancel_des = ""
    },
    handleDel(){
      if(!this.form.cancel_des.trim()){
        throwErrorTips("删除申请原因不能为空")
        return
      }
      if(this.is_handle){return}
      this.is_handle = true
      let d = JSON.parse(JSON.stringify(this.data))
      d.statu = '2'
      d.kind.forEach(v=>{
        v.num = v.num.toString()
      })
      d.cancel_des = this.form.cancel_des
      d["opt"] = "3"
      this.modAjax(d,"4","取消申领")
    },
    modAjax(data,skind,log_des){
      if(skind === "2"){
        data["opt"] = "2"
      }
      data["id"] = Number(data["id"])
      data["skind"] = skind
      data["log_des"] = log_des
      jQuery.ajax({
        url: postWasteLabelModUrl,
        type: 'post',
        data: JSON.stringify(data),
        contentType: "application/json",
        success: res => {
          if(res.success){
            this.$Message({
              message: "操作成功",
              type: 'success',
            })
            this.$emit("onclose")
          }else {
            throwErrorTips(res.msg)
          }
          this.is_handle = false
        },
        error:()=>{
          this.is_handle = false
          throwErrorTips("网络超时")
        }
      })
    },
    ...mapActions(["getWasteUserKs"])

  }
}
</script>
